package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.AreasRepository;
import com.lawyer.repository.CitiesRepository;
import com.lawyer.repository.ProvincesRepository;

@RestController
@RequestMapping(value = "/provinces")
@Api(tags = "省市区三级联动")
@SuppressWarnings("all")
public class ProvincesController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ProvincesRepository provincesRepository;
	@Autowired
	private AreasRepository areasRepository;
	@Autowired
	private CitiesRepository citiesRepository;

	@ApiOperation(httpMethod = "GET", value = "查询所有省份", notes = "查询所有省份")
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	public HttpEntity<?> query() {
		Message msg = new Message();
		try {
			msg.setData(provincesRepository.findAll());
		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "通过省份id查询该省下所有的市", notes = "通过省份id查询该省下所有的市")
	@RequestMapping(value = "/queryByProvinceid", method = RequestMethod.GET)
	public HttpEntity<?> queryByProvinceid(String provinceid) {
		Message msg = new Message();
		try {
			msg.setData(citiesRepository.findByProvinceid(provinceid));
		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "通过城市id查询该城市下所有的区", notes = "通过城市id查询该城市下所有的区")
	@RequestMapping(value = "/queryByCityid", method = RequestMethod.GET)
	public HttpEntity<?> queryByCityid(String cityid) {
		Message msg = new Message();
		try {
			msg.setData(areasRepository.findByCityid(cityid));
		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
}
